﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProdeFrontEnd.Clases;
using ProdeFrontEnd.ServiceReferenceProde;
using System.Data;

namespace ProdeFrontEnd.Pags
{
    public partial class MyForecast : System.Web.UI.Page
    {
        private ServiceProde serviceProde;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] == null) //Se tiene que salir
            {
                Response.Redirect("NewUser.aspx");
            }
            serviceProde = ServiceProde.getInstanceServiceProde();
            loadTournaments();
            loadUserResult();
            loadTournamentUser();
        } 

        private void loadTournaments()
        {
            Tournament[] tournaments = serviceProde.prodeClient.getTournaments();

            DataTable dataTable = new DataTable();
            dataTable.Columns.AddRange(new DataColumn[4] {
                                new DataColumn("tournament",typeof(string)),
                                new DataColumn("start",typeof(string)),
                                new DataColumn("finish",typeof(string)),
                                new DataColumn("idTournament",typeof(int)) });

            foreach (Tournament tournament in tournaments)
            {
                dataTable.Rows.Add(tournament.name, tournament.start.ToString("d"), tournament.end.ToString("d"), tournament.id);
            }

            GridView3.DataSource = dataTable;
            GridView3.DataBind();
        }

        private void loadTournamentUser()
        {


            Tournament[] tournaments = serviceProde.prodeClient.getTournamentByUser(((User)Session["User"]).id, "");

            DataTable dataTable = new DataTable();
            dataTable.Columns.AddRange(new DataColumn[3] {
                                new DataColumn("tournament",typeof(string)),
                                new DataColumn("start",typeof(string)),
                                new DataColumn("finish",typeof(string)) });

            foreach (Tournament tournament in tournaments)
            {
                dataTable.Rows.Add(tournament.name, tournament.start.ToString("d"), tournament.end.ToString("d"));
            }

            GridView2.DataSource = dataTable;
            GridView2.DataBind();
        }

        private void loadUserResult()
        {
            String filter = "idUser = " + ((User)Session["User"]).id;

            UserResult[] userResults = serviceProde.prodeClient.getUserResult(filter);

            DataTable dataTable = new DataTable();
            dataTable.Columns.AddRange(new DataColumn[8] {
                                new DataColumn("pathFlag1",typeof(string)),
                                new DataColumn("team1",typeof(string)),
                                new DataColumn("VTeam1",typeof(int)),
                                new DataColumn("vs", typeof(string)),
                                new DataColumn("VTeam2",typeof(int)),
                                new DataColumn("team2",typeof(string)),
                                new DataColumn("pathFlag2",typeof(string)),
                                new DataColumn("tournament",typeof(string))});

            foreach (UserResult userResult in userResults)
            {
                Match match = userResult.match;
                Team team1 = match.team1;
                Team team2 = match.team2;
                Tournament tournament = match.tournamente;
                dataTable.Rows.Add(team1.pathFlag, team1.name, userResult.VTeam1, "-", userResult.VTeam2, team2.name, team2.pathFlag, tournament.name);
            }

            GridView1.DataSource = dataTable;
            GridView1.DataBind();
        }

        protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "signon")
            {
                int idTournament = Convert.ToInt32(e.CommandArgument);
                serviceProde.prodeClient.signonUserInTeam(((User)Session["User"]), idTournament);
            }
        }
    }
}